<template><div><h2 id="环境信息" tabindex="-1"><a class="header-anchor" href="#环境信息"><span>环境信息</span></a></h2>
<p>Ubuntu 22-24 应该都是可以的<br>
安装的mysql为8.0版本</p>
<p><em><strong>2025/8/30更简单的安装方式</strong></em></p>
<blockquote>
<p><a href="https://blog.csdn.net/yzf279533105/article/details/137694027" target="_blank" rel="noopener noreferrer">【最新】非常详细 ubuntu24安装mysql 8.0保姆级教程_ubuntu安装mysql8.0-CSDN博客</a></p>
</blockquote>
<p><em><strong>ubuntu防火墙ufw使用方式</strong></em></p>
<blockquote>
<p><a href="https://wker.com/ufw-tutorial/" target="_blank" rel="noopener noreferrer">ufw 完整使用教程 - 玩客</a></p>
</blockquote>
<h2 id="安装mysql" tabindex="-1"><a class="header-anchor" href="#安装mysql"><span>安装mysql</span></a></h2>
<div class="language-bash line-numbers-mode" data-highlighter="shiki" data-ext="bash" style="background-color:#282c34;color:#abb2bf"><pre class="shiki one-dark-pro vp-code" v-pre=""><code><span class="line"><span style="color:#61AFEF">sudo</span><span style="color:#98C379"> apt-get</span><span style="color:#98C379"> update</span><span style="color:#7F848E;font-style:italic"> # 更新安装包工具</span></span>
<span class="line"><span style="color:#61AFEF">sudo</span><span style="color:#98C379"> apt-get</span><span style="color:#98C379"> install</span><span style="color:#98C379"> mysql-server</span><span style="color:#7F848E;font-style:italic"> # 安装mysql 确认按y</span></span></code></pre>
<div class="line-numbers" aria-hidden="true" style="counter-reset:line-number 0"><div class="line-number"></div><div class="line-number"></div></div></div><h2 id="启动mysql" tabindex="-1"><a class="header-anchor" href="#启动mysql"><span>启动mysql</span></a></h2>
<div class="language-bash line-numbers-mode" data-highlighter="shiki" data-ext="bash" style="background-color:#282c34;color:#abb2bf"><pre class="shiki one-dark-pro vp-code" v-pre=""><code><span class="line"><span style="color:#61AFEF">sudo</span><span style="color:#98C379"> service</span><span style="color:#98C379"> mysql</span><span style="color:#98C379"> start</span><span style="color:#7F848E;font-style:italic"> # 启动mysql</span></span>
<span class="line"><span style="color:#7F848E;font-style:italic"># sudo service mysql stop 停止mysql</span></span>
<span class="line"><span style="color:#61AFEF">sudo</span><span style="color:#98C379"> service</span><span style="color:#98C379"> mysql</span><span style="color:#98C379"> status</span><span style="color:#7F848E;font-style:italic"> # 查看mysql状态</span></span></code></pre>
<div class="line-numbers" aria-hidden="true" style="counter-reset:line-number 0"><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div></div></div><h2 id="mysql开机自启" tabindex="-1"><a class="header-anchor" href="#mysql开机自启"><span>mysql开机自启</span></a></h2>
<div class="language-bash line-numbers-mode" data-highlighter="shiki" data-ext="bash" style="background-color:#282c34;color:#abb2bf"><pre class="shiki one-dark-pro vp-code" v-pre=""><code><span class="line"><span style="color:#61AFEF">sudo</span><span style="color:#98C379"> systemctl</span><span style="color:#98C379"> enable</span><span style="color:#98C379"> mysql</span></span></code></pre>
<div class="line-numbers" aria-hidden="true" style="counter-reset:line-number 0"><div class="line-number"></div></div></div><h2 id="配置mysql登录权限" tabindex="-1"><a class="header-anchor" href="#配置mysql登录权限"><span>配置mysql登录权限</span></a></h2>
<h3 id="mysql登录" tabindex="-1"><a class="header-anchor" href="#mysql登录"><span>mysql登录</span></a></h3>
<div class="language-bash line-numbers-mode" data-highlighter="shiki" data-ext="bash" style="background-color:#282c34;color:#abb2bf"><pre class="shiki one-dark-pro vp-code" v-pre=""><code><span class="line"><span style="color:#61AFEF">mysql</span><span style="color:#D19A66"> -u</span><span style="color:#98C379"> root</span><span style="color:#D19A66"> -p</span><span style="color:#7F848E;font-style:italic"> # 第一登录没有密码，直接回车</span></span></code></pre>
<div class="line-numbers" aria-hidden="true" style="counter-reset:line-number 0"><div class="line-number"></div></div></div><h3 id="设置root用户连接密码" tabindex="-1"><a class="header-anchor" href="#设置root用户连接密码"><span>设置root用户连接密码</span></a></h3>
<div class="language-bash line-numbers-mode" data-highlighter="shiki" data-ext="bash" style="background-color:#282c34;color:#abb2bf"><pre class="shiki one-dark-pro vp-code" v-pre=""><code><span class="line"><span style="color:#61AFEF">use</span><span style="color:#98C379"> mysql</span><span style="color:#ABB2BF">; </span><span style="color:#7F848E;font-style:italic"># 进入mysql</span></span>
<span class="line"></span>
<span class="line"><span style="color:#C678DD">select</span><span style="color:#E06C75"> host,</span><span style="color:#ABB2BF"> user, authentication_string from user; </span><span style="color:#7F848E;font-style:italic"># 查看需要修改的root用户数据</span></span>
<span class="line"></span>
<span class="line"><span style="color:#61AFEF">ALTER</span><span style="color:#98C379"> USER</span><span style="color:#98C379"> 'root'@'localhost'</span><span style="color:#98C379"> IDENTIFIED</span><span style="color:#98C379"> WITH</span><span style="color:#98C379"> mysql_native_password</span><span style="color:#98C379"> BY</span><span style="color:#98C379"> '设置新的密码'</span><span style="color:#ABB2BF">; </span><span style="color:#7F848E;font-style:italic"># 为root账号赋权</span></span>
<span class="line"></span>
<span class="line"><span style="color:#61AFEF">FLUSH</span><span style="color:#98C379"> PRIVILEGES</span><span style="color:#ABB2BF">; </span><span style="color:#7F848E;font-style:italic"># 刷新权限</span></span>
<span class="line"></span>
<span class="line"><span style="color:#56B6C2">exit</span><span style="color:#ABB2BF">;  </span><span style="color:#7F848E;font-style:italic"># 推出mysql控制台</span></span></code></pre>
<div class="line-numbers" aria-hidden="true" style="counter-reset:line-number 0"><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div></div></div><h3 id="设置远程访问登录" tabindex="-1"><a class="header-anchor" href="#设置远程访问登录"><span>设置远程访问登录</span></a></h3>
<p>设置账号权限</p>
<div class="language-bash line-numbers-mode" data-highlighter="shiki" data-ext="bash" style="background-color:#282c34;color:#abb2bf"><pre class="shiki one-dark-pro vp-code" v-pre=""><code><span class="line"><span style="color:#7F848E;font-style:italic">#进入mysql</span></span>
<span class="line"><span style="color:#61AFEF">use</span><span style="color:#98C379"> mysql</span><span style="color:#ABB2BF">;</span></span>
<span class="line"><span style="color:#ABB2BF"> </span></span>
<span class="line"><span style="color:#7F848E;font-style:italic">#设置登录ip为%通配符，取消登录ip限制</span></span>
<span class="line"><span style="color:#61AFEF">update</span><span style="color:#98C379"> user</span><span style="color:#98C379"> set</span><span style="color:#98C379"> host</span><span style="color:#98C379"> =</span><span style="color:#98C379"> '%'</span><span style="color:#98C379"> where</span><span style="color:#98C379"> user</span><span style="color:#98C379"> =</span><span style="color:#98C379"> 'root'</span><span style="color:#98C379"> and</span><span style="color:#98C379"> host</span><span style="color:#98C379"> =</span><span style="color:#98C379"> 'localhost'</span><span style="color:#ABB2BF">;</span></span>
<span class="line"><span style="color:#ABB2BF"> </span></span>
<span class="line"><span style="color:#7F848E;font-style:italic">#刷新权限</span></span>
<span class="line"><span style="color:#61AFEF">FLUSH</span><span style="color:#98C379"> PRIVILEGES</span><span style="color:#ABB2BF">;</span></span>
<span class="line"><span style="color:#ABB2BF"> </span></span>
<span class="line"><span style="color:#7F848E;font-style:italic">#查看数据情况</span></span>
<span class="line"><span style="color:#C678DD">select</span><span style="color:#E06C75"> host,</span><span style="color:#ABB2BF"> user, authentication_string from user;</span></span>
<span class="line"><span style="color:#ABB2BF"> </span></span>
<span class="line"><span style="color:#7F848E;font-style:italic">#退出mysql控制台</span></span>
<span class="line"><span style="color:#56B6C2">exit</span><span style="color:#ABB2BF">;</span></span></code></pre>
<div class="line-numbers" aria-hidden="true" style="counter-reset:line-number 0"><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div></div></div><p>修改配置文件</p>
<div class="language-bash line-numbers-mode" data-highlighter="shiki" data-ext="bash" style="background-color:#282c34;color:#abb2bf"><pre class="shiki one-dark-pro vp-code" v-pre=""><code><span class="line"><span style="color:#7F848E;font-style:italic">#修改mysql配置文件，取消对远程ip为127.0.0.1的绑定</span></span>
<span class="line"><span style="color:#7F848E;font-style:italic">#将bind-address、mysqlx-bind-address这两行注释掉</span></span>
<span class="line"><span style="color:#61AFEF">vim</span><span style="color:#98C379"> /etc/mysql/mysql.conf.d/mysqld.cnf</span></span></code></pre>
<div class="line-numbers" aria-hidden="true" style="counter-reset:line-number 0"><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div></div></div><p>重启mysql</p>
<div class="language-bash line-numbers-mode" data-highlighter="shiki" data-ext="bash" style="background-color:#282c34;color:#abb2bf"><pre class="shiki one-dark-pro vp-code" v-pre=""><code><span class="line"><span style="color:#61AFEF">service</span><span style="color:#98C379"> mysql</span><span style="color:#98C379"> restart</span></span></code></pre>
<div class="line-numbers" aria-hidden="true" style="counter-reset:line-number 0"><div class="line-number"></div></div></div><h2 id="防火墙放行" tabindex="-1"><a class="header-anchor" href="#防火墙放行"><span>防火墙放行</span></a></h2>
<p>如果是云服务器，还需要在安全组中开放3306端口</p>
<div class="language-bash line-numbers-mode" data-highlighter="shiki" data-ext="bash" style="background-color:#282c34;color:#abb2bf"><pre class="shiki one-dark-pro vp-code" v-pre=""><code><span class="line"><span style="color:#7F848E;font-style:italic">#服务器防火墙开放3306端口</span></span>
<span class="line"><span style="color:#61AFEF">sudo</span><span style="color:#98C379"> ufw</span><span style="color:#98C379"> allow</span><span style="color:#98C379"> 3306/tcp</span></span></code></pre>
<div class="line-numbers" aria-hidden="true" style="counter-reset:line-number 0"><div class="line-number"></div><div class="line-number"></div></div></div></div></template>


